package com.mycompany.j2eem.controller;

import com.mycompany.j2eem.Entity.JsonResult;
import com.mycompany.j2eem.Entity.ResultCode;
import com.mycompany.j2eem.Entity.bean.TagObjectBean;
import com.mycompany.j2eem.service.wrjservice.RecommendationService;
import com.mycompany.j2eem.service.wrjservice.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class TagController {

    @Autowired
    RecommendationService recommendationService;
    @Autowired
    SearchService searchService;

    //热门标签
    @RequestMapping("/HotTag")//ok
    public JsonResult hotTag() {

        List<String> hotTagList = recommendationService.getHotTagIdService();
        if (hotTagList == null) {
            return new JsonResult(ResultCode.NOT_DATA,"失败");
        }

        return new JsonResult(ResultCode.SUCCESS, "成功", hotTagList);
    }

    //个人推荐标签
    @RequestMapping("/PersonalTag")
    public JsonResult personalTag(HttpSession session) {

        boolean isLogin = true;
        if (isLogin) {
            String userName = session.getAttribute("username").toString();
            List<TagObjectBean> personalTagList =recommendationService.recommend_tag_service(userName,true) ;
            if (personalTagList == null) {
                return new JsonResult(ResultCode.NOT_DATA);
            }
            return new JsonResult(ResultCode.SUCCESS, "成功", personalTagList);
        }

        return hotTag();

    }

    //返回主标签（主要的几个标签）
    @RequestMapping("/MainTab")//ok
    public JsonResult MainTab() {
        List<String> mainTagList = searchService.getPointedTags(true);
        if (mainTagList == null) {
            return new JsonResult(ResultCode.NOT_DATA);
        }

        return new JsonResult(ResultCode.SUCCESS, "成功", mainTagList);
    }

    //根据选择的主标签返回子标签
    @RequestMapping("/ChildTag")//ok
    public JsonResult childTag(@RequestParam("parentTag") String parentTag) {

//        try {
//            parentTag=new String(parentTag.getBytes("ISO-8859-1"), "UTF-8");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
        List<String> childTagList = searchService.getRelationalTags(parentTag);
        if (childTagList == null) {
            return new JsonResult(ResultCode.NOT_DATA);
        }

        return new JsonResult(ResultCode.SUCCESS, "成功", childTagList);
    }

    //根据字符返回可能存在的标签
    @RequestMapping("/TagTipsWhileSearching")//ok
    public JsonResult tagTipsWhileSearching(@RequestParam("keywords") String keywords) {
        //TODO
        //获取可能的标签
        //返回格式如下
//        try {
//            keywords=new String(keywords.getBytes("ISO-8859-1"), "UTF-8");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
        /*******正式之前把这个改了      ***************************************************************    */
        List<String> availTags=searchService.searchForTag(keywords);
        List<String> availNames=searchService.getTagNames(availTags);
        if (availNames == null) {
            return new JsonResult(ResultCode.NOT_DATA);
        }

        return new JsonResult(ResultCode.SUCCESS, "成功", availNames);
//
//        List<String> tagTipsList = new ArrayList<>();
//        tagTipsList.add("test1");
//        tagTipsList.add("test2");
//        tagTipsList.add("test3");
//
//        if (tagTipsList == null) {
//            return new JsonResult(ResultCode.NOT_DATA);
//        }
//
//        return new JsonResult(ResultCode.SUCCESS, "成功", tagTipsList);
    }

}
